package serviceparameterisunique;

import java.util.Arrays;
import org.eclipse.viatra2.emf.incquery.runtime.api.IPatternMatch;
import org.eclipse.viatra2.emf.incquery.runtime.api.impl.BasePatternMatch;
import org.eclipse.viatra2.patternlanguage.core.patternLanguage.Pattern;
import sysmodel.Service;
import sysmodel.ServiceParameter;

/**
 * Pattern-specific match representation of the ServiceParameterIsUnique pattern, 
 * to be used in conjunction with ServiceParameterIsUniqueMatcher.
 * 
 * <p>Class fields correspond to parameters of the pattern. Fields with value null are considered unassigned.
 * Each instance is a (possibly partial) substitution of pattern parameters, 
 * usable to represent a match of the pattern in the result of a query, 
 * or to specify the bound (fixed) input parameters when issuing a query.
 * 
 * @see ServiceParameterIsUniqueMatcher
 * @see ServiceParameterIsUniqueProcessor
 * 
 */
public final class ServiceParameterIsUniqueMatch extends BasePatternMatch implements IPatternMatch {
  private Service fService;
  
  private ServiceParameter fServiceParameter;
  
  private static String[] parameterNames = {"service", "serviceParameter"};
  
  public ServiceParameterIsUniqueMatch(final Service pService, final ServiceParameter pServiceParameter) {
    this.fService = pService;
    this.fServiceParameter = pServiceParameter;
    
  }
  
  @Override
  public Object get(final String parameterName) {
    if ("service".equals(parameterName)) return this.fService;
    if ("serviceParameter".equals(parameterName)) return this.fServiceParameter;
    return null;
    
  }
  
  public Service getService() {
    return this.fService;
    
  }
  
  public ServiceParameter getServiceParameter() {
    return this.fServiceParameter;
    
  }
  
  @Override
  public boolean set(final String parameterName, final Object newValue) {
    if ("service".equals(parameterName) && newValue instanceof sysmodel.Service) {
    	this.fService = (sysmodel.Service) newValue;
    	return true;
    }
    if ("serviceParameter".equals(parameterName) && newValue instanceof sysmodel.ServiceParameter) {
    	this.fServiceParameter = (sysmodel.ServiceParameter) newValue;
    	return true;
    }
    return false;
    
  }
  
  public void setService(final Service pService) {
    this.fService = pService;
    
  }
  
  public void setServiceParameter(final ServiceParameter pServiceParameter) {
    this.fServiceParameter = pServiceParameter;
    
  }
  
  @Override
  public String patternName() {
    return "ServiceParameterIsUnique";
    
  }
  
  @Override
  public String[] parameterNames() {
    return ServiceParameterIsUniqueMatch.parameterNames;
    
  }
  
  @Override
  public Object[] toArray() {
    return new Object[]{fService, fServiceParameter};
    
  }
  
  @Override
  public String prettyPrint() {
    StringBuilder result = new StringBuilder();
    result.append("\"service\"=" + prettyPrintValue(fService) + ", ");
    
    result.append("\"serviceParameter\"=" + prettyPrintValue(fServiceParameter)
    );return result.toString();
    
  }
  
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((fService == null) ? 0 : fService.hashCode()); 
    result = prime * result + ((fServiceParameter == null) ? 0 : fServiceParameter.hashCode()); 
    return result; 
    
  }
  
  @Override
  public boolean equals(final Object obj) {
    if (this == obj)
    	return true;
    if (obj == null)
    	return false;
    if (!(obj instanceof IPatternMatch))
    	return false;
    IPatternMatch otherSig  = (IPatternMatch) obj;
    if (!pattern().equals(otherSig.pattern()))
    	return false;
    if (!ServiceParameterIsUniqueMatch.class.equals(obj.getClass()))
    	return Arrays.deepEquals(toArray(), otherSig.toArray());
    				ServiceParameterIsUniqueMatch other = (ServiceParameterIsUniqueMatch) obj;
    				if (fService == null) {if (other.fService != null) return false;}
    				else if (!fService.equals(other.fService)) return false;
    				if (fServiceParameter == null) {if (other.fServiceParameter != null) return false;}
    				else if (!fServiceParameter.equals(other.fServiceParameter)) return false;
    				return true;
    
  }
  
  @Override
  public Pattern pattern() {
    return ServiceParameterIsUniqueMatcher.FACTORY.getPattern();
  }
}
